* ********************************************************************************************
* The Effects of Weather Shocks on Economic Activity: What are the Channels of Impact?
* Sebastian Acevedo, Mico Mrkaic, Natalija Novta, Evgenia Pugacheva, Petia Topalova
* With support from Gavin Asdorian, Olivia Ma, Jilun Xing and Yuan Zeng 
* Replication files for Table 4; Figure 8
* ********************************************************************************************
clear all
set more off
set matsize 11000

cd "C:\Users\..\replication" // set your working directory to where the replication folder is saved

* the Y variable that will be used in regressions
local Y_list ava_rlcu_wdi ag_prd_crop mva_rlcu_wdi sva_rlcu_wdi ni_r nm_r mrt_i hdi

* Horizons
global k 7

* ****************************************************************************************
* Data
* ****************************************************************************************
use "data/subnational_dataset.dta", clear

* temperature, keep sample within [15; 20]
bys provcode: egen mn_pwtemp = mean(pwtemp)
keep if inrange(mn_pwtemp, 15, 20)

foreach X in pwtemp pwprecip {
	gen `X'2=`X'^2
}

gen sqyear=year^2

by provcode (year), sort: gen d0_ln_gdppc = ln_gdppc - l.ln_gdppc
forval t = 1/$k {
	by provcode (year), sort: gen d`t'_ln_gdppc = f`t'.ln_gdppc - l.ln_gdppc
}

* Create WDI region year fixed effects
gen tempvar1 = wdi_region + " " + string(year) if wdi_region != ""
encode tempvar1, generate(ry)
drop tempvar1

xi i.ifscode*year i.ifscode*sqyear  i.ry i.year
* interact the RY with the AE dummy
xi i.ae*i.ry
gen l_d0_ln_gdppcl_ae = ae* l.d0_ln_gdppc

* ****************************************************************************************
* Variables where to store results
* ****************************************************************************************
gen period = -1 in 1
foreach X in pwtemp pwprecip{
	gen `X'_coef = 0 in 1
	gen `X'_se = 0 in 1
	gen `X'_pvalue = . in 1
}

gen nobs = . in 1
gen nprov = . in 1
gen ncountry = . in 1
gen year_start = . in 1
gen year_end = . in 1
gen r2 = . in 1

* ****************************************************************************************
* Program
* ****************************************************************************************

forval t = 0/$k {
	areg d`t'_ln_gdppc pwtemp pwprecip l.pwtemp l.pwprecip l.d0_ln_gdppc _Iry_*, cluster(provcode) absorb(provcode)

	foreach X in pwtemp pwprecip {
		replace `X'_coef = _b[`X'] in `=`t'+2'
		replace `X'_se = _se[`X'] in `=`t'+2'
		replace `X'_pvalue = 2 * ttail(e(df_r),abs(_b[`X']/_se[`X'])) in `=`t'+2'
	}

	replace period = `t' in `=`t'+2'

	replace nobs = `e(N)' in `=`t'+2'

	* number of provinces
	replace nprov = `e(N_clust)' in `=`t'+2'

	* number of countries
	levelsof ifscode if e(sample), local(country_list)
	local country_count: list sizeof country_list
	replace ncountry = `country_count' in `=`t'+2'

	* Years of the sample
	qui sum year if e(sample)
	replace year_start = r(min) in `=`t'+2'
	replace year_end = r(max) in `=`t'+2'

	* r squared
	replace r2 = e(r2) in `=`t'+2'
}

export excel period - r2 in 1/9 using "output/Table_4.xlsx", sheet("column_3") sheetreplace firstrow(var)
